package components.contorls.superClass
{
    import components.core.UIComponent;
    import components.core.zn_internal;
    
    import flash.display.MovieClip;
    import flash.display.Shape;

    public class BaseProgressBar extends UIComponent
    {
        protected var _barMask:Shape;

        private var _percent:Number = 0.5;

        private var _autoSetLabel:Boolean = true;
		
		/**
		 *是否显示进度标签 
		 */		
		private var _showLabel:Boolean=true;

		protected var _barLeft:Number;
		protected var _barRight:Number;
		
        public function BaseProgressBar()
        {
            super();
			
            if (labelTF)
                labelTF.horizontalCenter = 0;

        }

        /***************************************
         *
         * 显示对象声明
         *
         * **************************************/

        public function get labelTF():BaseLabel
        {
            return getChildByName("labelSkin") as BaseLabel;
        }

        public function get trackMC():MovieClip
        {
            return getChildByName("trackSkin") as MovieClip;
        }

        public function get barMC():MovieClip
        {
            return getChildByName("barSkin") as MovieClip
        }

        /***************************************
         *
         * 验证
         *
         * **************************************/

        protected override function commitProperties():void
        {
            super.commitProperties();

            if (!_initialized)
            {
                _barMask = new Shape();
                _barMask.graphics.beginFill(0, 1);
                _barMask.graphics.drawRect(0, 0, 1, 1);
                _barMask.graphics.endFill();
                addChild(_barMask);
				
                _barMask.x = barMC.x;
                _barMask.y = barMC.y;

                _barMask.height = barMC.height;

                barMC.mask = _barMask;
				
				_barLeft=barMC.x;
				_barRight=_normalWidth-barMC.x-barMC.width;
            }
			
			if (labelTF)
				labelTF.visible=showLabel;
        }

        protected override function measureSizes():Boolean
        {
            var change:Boolean = super.measureSizes();
            if (change)
            {
                trackMC.width = width;
                barMC.width =width-_barLeft-_barRight;

                if (labelTF)
                    labelTF.invalidate();
            }

            _barMask.width = percent * barMC.width;

            return change;
        }

        protected override function updateDisplayList():void
        {
            super.updateDisplayList();

            if (labelTF && autoSetLabel)
                labelTF.text = String(Math.round(percent * 100)) + " %";
        }

        /***************************************
         *
         * 组件定义
         *
         * **************************************/


        [Inspectable(defaultValue="0.5")]
        public function get percent():Number
        {
            return _percent;
        }

        public function set percent(value:Number):void
        {
            _percent = value;

            if (_percent > 1)
                _percent = 1;
            else if (_percent < 0 || isNaN(_percent))
                _percent = 0;
			
            invalidate();
        }

        [Inspectable(defaultValue="true")]
        public function get autoSetLabel():Boolean
        {
            return _autoSetLabel;
        }

        public function set autoSetLabel(value:Boolean):void
        {
            _autoSetLabel = value;
        }

		[Inspectable(defaultValue="true")]
		public function get showLabel():Boolean
		{
			return _showLabel;
		}
		
		public function set showLabel(value:Boolean):void
		{
			_showLabel = value;
			invalidate();
		}
		
        /***************************************
         *
         * 其他代码
         *
         * **************************************/


        public override function set focusComp(value:UIComponent):void
        {
            super.focusComp = value;

            if (labelTF)
                labelTF.focusComp = focusComp;
        }

        public override function destroy():void
        {
            super.destroy();
            _barMask = null;
        }

        public override function validateNow():void
        {
            super.validateNow();
            if (labelTF)
                labelTF.validateNow();
        }
    }
}